<?php

class AcessoController extends Zend_Controller_Action
{
	public function init()
	{

	}

	public function indexAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->redirect('/acesso/login');
		
	}

	
	public function loginAction(){

		$tUsuario = new DbTable_Usuario();
		
		
		
		if ((!is_null($this->getParam('id_facebook'))) || (!is_null($this->getParam('login')) && !is_null($this->getParam('password')))){
				
			
			if (!is_null($this->getParam('id_facebook'))){
				$usuario = $tUsuario->fetchAll(array(	"ds_email = (?)" => strtoupper($this->getParam('login')),
						"id_user_facebook = (?)" => $this->getParam('id_facebook')))->current();
				
				if (is_null($usuario->id_user_facebook)) $this->redirect('/acesso/login/invalido/facebook');
			}else{
				
				$usuario = $tUsuario->fetchAll(array(	"ds_email = (?)" => strtoupper($this->getParam('login')),
						"ds_senha = (?)" => sha1($this->getParam('password'))))->current();
			}
					
			$sessionRole = new Zend_Session_Namespace('role');
			
			$tipoUsuario = $usuario->tp_usuario;
			
			if (is_null($tipoUsuario)){
				$this->redirect('/acesso/login/invalido/true');
			}elseif ($tipoUsuario == 'A'){
				Zend_Registry::set('perfilCorrente', 'ADMINISTRADOR');
			}elseif($tipoUsuario == 'C'){
				Zend_Registry::set('perfilCorrente', 'CLIENTE');
			}
                        
			$sessionRole->idUsuario = $usuario->id_usuario;
			$sessionRole->role = Zend_Controller_Action_HelperBroker::getStaticHelper('role')->nameToAlias(Zend_Registry::get('perfilCorrente'));
			
                        if ($this->getParam('reset'))
                            $this->redirect("/index/index/reset/" . Messages::RESET_SENHA);
                        else
                            $this->redirect("/index");
		}
		
		if ($this->_getParam('invalido') == 'facebook'){
			$this->view->message = Messages::LOGIN_FACEBOOK_SEM_LINK;
		}else if (!is_null($this->_getParam('invalido')))
			$this->view->message = Messages::LOGIN_USUARIO_INCORRETO;
	}	
	
	
	public function logoutAction(){
		$this->_helper->viewRenderer->setNoRender();
		$sessionRole = new Zend_Session_Namespace('role');
		$sessionRole->role = Zend_Controller_Action_HelperBroker::getStaticHelper('role')->nameToAlias('GUEST');
		$this->redirect('/acesso/login');
	}
	
}

